﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfContrib.Utils;

namespace WcfContrib.Extensions.Caching
{
    /// <summary>
    /// Represents a store for ouput caching for a synchronized in-proc in-memory cache
    /// </summary>
    public class InProcOutputCacheStore : IOutputCacheStore
    {
        SynchronizedDictionary<OutputCacheItemKey, object> store = new SynchronizedDictionary<OutputCacheItemKey, object>();

        #region IOutputCacheStore Members

        /// <summary>
        /// Adds or replaces the item in the store.
        /// </summary>
        /// <param name="key">The item key.</param>
        /// <param name="value">The value.</param>
        public void SetItem(OutputCacheItemKey key, object value)
        {
            store[key] = value;
        }

        /// <summary>
        /// Removes the item from the store.
        /// </summary>
        /// <param name="key">The item key.</param>
        public void RemoveItem(OutputCacheItemKey key)
        {
            store.Remove(key);
        }

        /// <summary>
        /// Tries to get the item from the store.
        /// </summary>
        /// <param name="key">The item key.</param>
        /// <param name="value">The value.</param>
        /// <returns>
        /// true if the item was found; otherwise, false
        /// </returns>
        public bool TryGetItem(OutputCacheItemKey key, out object value)
        {
            return store.TryGetValue(key, out value);
        }

        #endregion
    }
}
